﻿Public Class frm_SearchWindow
    Dim frm As frm_TextEdit
#Region "窗体构造函数"
    Public Sub New(ByVal frm As frm_TextEdit)

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。
        frm = frm
    End Sub
    Public Sub New()

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。

    End Sub
    Public Sub New(ByVal sText As String)
        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。
        TB_SearchValue.Text = sText
    End Sub
    Public Sub New(ByVal frm As frm_TextEdit, ByVal gText As String)

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。
        frm = frm
        TB_SearchValue.Text = gText
    End Sub
#End Region
    Public Property FatherForm() As frm_TextEdit
        Get
            Return frm
        End Get
        Set(ByVal value As frm_TextEdit)
            frm = value
        End Set
    End Property
    Dim SearchIndex As Long
    Private Sub frm_SearchReplace_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SearchIndex = 0
        RB_DirectionDown.Checked = True
    End Sub

    Private Sub Btn_Search_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Search.Click
        Dim SearchValue As String = TranslateText(TB_SearchValue.Text)
        Dim SearchOption As New System.Windows.Forms.RichTextBoxFinds
        If CB_UpowerdLower.Checked Then
            SearchOption = RichTextBoxFinds.MatchCase
        Else
            SearchOption = RichTextBoxFinds.None
        End If
        With DirectCast(frm, frm_TextEdit)
            If RB_DirectionDown.Checked Then
                If SearchIndex < 0 Then SearchIndex = 0
                SearchIndex = .TextBox.Find(SearchValue, SearchIndex, .TextBox.TextLength, SearchOption)
                If SearchIndex > 0 Then
                    .TextBox.SelectionStart = SearchIndex
                    .TextBox.SelectionLength = SearchValue.Length
                    SearchIndex = SearchIndex + SearchValue.Length
                Else
                    MessageBox.Show(Me, "已搜索至文档结束！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
                End If
            Else
                If SearchIndex <= 0 Then SearchIndex = .TextBox.TextLength
                SearchIndex = FindbyPresve(SearchValue)
                If SearchIndex > 0 Then
                    .TextBox.SelectionStart = SearchIndex
                    .TextBox.SelectionLength = SearchValue.Length
                Else
                    MessageBox.Show(Me, "已搜索至文档起始位置！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
                End If
            End If
        End With
    End Sub
    Private Function FindbyPresve(ByVal value As String) As Long
        Dim ToSearch As String = frm.TextBox.Text.Substring(0, SearchIndex)
        Dim Result As Integer = -1
        If CB_UpowerdLower.Checked Then
            Result = ToSearch.LastIndexOf(value)
        Else
            Result = ToSearch.ToLower.LastIndexOf(value.ToLower)
        End If
        Return Result
    End Function
    Private Sub Btn_Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Cancel.Click
        Me.Close()
    End Sub
End Class